<?php

class FeedController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {
        // action body
        $this->_helper->Layout->disableLayout();
        $feed = new Zend_Feed_Writer_Feed;
        $this->getResponse()
                ->setHeader('Content-type', 'application/xml');
        $feed->setEncoding("utf-8");
        if ($_SERVER['SERVER_NAME'] == 'si.hathmaluwa.org') {
            $language = 'si';
        } elseif ($_SERVER['SERVER_NAME'] == 'ta.hathmaluwa.org') {
            $language = 'ta';
        } elseif ($_SERVER['SERVER_NAME'] == 'en.hathmaluwa.org') {
            $language = 'en';
        } else {
            $language = 'all';
        }

        if ($language == 'si') {
            $feed->setTitle('Hathmaluwa Sinhala RSS Feed');
        } elseif ($language == 'ta') {
            $feed->setTitle('Hathmaluwa Tamil RSS Feed');
        } elseif ($language == 'en') {
            $feed->setTitle('Hathmaluwa English RSS Feed');
        } else {
            $feed->setTitle('Hathmaluwa All Languages RSS Feed');
        }
        $feed->setLink('http://' . $_SERVER['SERVER_NAME']."/");
        $feed->setFeedLink('http://' . $_SERVER['SERVER_NAME'] . '/feed', 'atom');
        $feed->setDateModified(time());
        $feed->addHub('http://pubsubhubbub.appspot.com/');
        $feed->setDescription("The Sri Lankan Trilingual Blog Syndicator");
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $query = $db->select()
                        ->from('blog_post_info', array(new Zend_Db_Expr('SQL_CALC_FOUND_ROWS post_id AS id'), 'posted_time', 'blog_post_summery', 'blog_post_title', 'blog_post_url','author'))
                        ->joinLeft('blog_info', "blog_info.blog_id=blog_post_info.blog_id", array('blog_name', 'blog_url', 'language'))
                        ->order('posted_time DESC')
                        ->where('blog_info.status = ?', 'approved')
                        ->where('posted_time >= SUBDATE(now(), interval 1 day)');
        if ($language != 'all') {
            $query->where('blog_info.language =?', $language);
        }
        $result = $db->fetchAll($query);
        $feeddata = new Application_Model_FeedData();
        foreach ($result as $res) {
            $entry = $feed->createEntry();
            $entry->setTitle($res['blog_post_title']);
            $entry->setLink($feeddata->encodeURI($res['blog_post_url']));
//            $entry->setId($feeddata->encodeURI($res['blog_post_url']));
//            $entry->addAuthor(array(
//                'name' => 'Paddy',
//                'email' => 'paddy@example.com',
//                'uri' => 'http://www.example.com',
//            ));
            if(empty($res['author'])){
                $entry->addAuthor('undefined');
            }else{
                $entry->addAuthor($res['author']);
            }
            $entry->setDateModified(strtotime($res['posted_time']));
            $entry->setDateCreated(strtotime($res['posted_time']));
            if (empty($res['blog_post_summery'])) {
                $entry->setDescription("empty");
                $entry->setContent("empty");
            } else {
                $entry->setDescription($res['blog_post_summery']);
                $entry->setContent($res['blog_post_summery']);
            }


            $feed->addEntry($entry);
        }

        $this->view->out = $feed->export('atom');
    }

}

